<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="java对象1234567891011// 创建类——“人”public class People&amp;#123;    int age;&amp;#125;// 创建类——“男人”public class MenPeople extends People &amp;#123;    &amp;#125;// 创建对象MenPeople xm = new MenPeople();xm.age = 15;  js对象var x">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaScript之面向对象">
<meta property="og:url" content="http://yoursite.com/2020/12/07/JavaScript之面向对象/index.html">
<meta property="og:site_name" content="春风自是人间客">
<meta property="og:description" content="java对象1234567891011// 创建类——“人”public class People&amp;#123;    int age;&amp;#125;// 创建类——“男人”public class MenPeople extends People &amp;#123;    &amp;#125;// 创建对象MenPeople xm = new MenPeople();xm.age = 15;  js对象var x">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://wx2.sinaimg.cn/mw690/0069qZtTgy1gho48t4y6vj30970i275q.jpg">
<meta property="og:image" content="https://wx1.sinaimg.cn/mw690/0069qZtTgy1gho49dgk5zj30d30irjt4.jpg">
<meta property="og:image" content="https://wx1.sinaimg.cn/mw690/0069qZtTgy1gho49w7kobj306102wjra.jpg">
<meta property="og:updated_time" content="2020-12-08T13:25:25.757Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JavaScript之面向对象">
<meta name="twitter:description" content="java对象1234567891011// 创建类——“人”public class People&amp;#123;    int age;&amp;#125;// 创建类——“男人”public class MenPeople extends People &amp;#123;    &amp;#125;// 创建对象MenPeople xm = new MenPeople();xm.age = 15;  js对象var x">
<meta name="twitter:image" content="https://wx2.sinaimg.cn/mw690/0069qZtTgy1gho48t4y6vj30970i275q.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: "",
      labels: ""
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2020/12/07/JavaScript之面向对象/">





  <title>JavaScript之面向对象 | 春风自是人间客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">春风自是人间客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">是非成败转头空，青山依旧在，几度夕阳红。</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-program">
          <a href="/categories/program" rel="section">
            
            程序
          </a>
        </li>
      
        
        <li class="menu-item menu-item-reading">
          <a href="/categories/reading" rel="section">
            
            阅读
          </a>
        </li>
      
        
        <li class="menu-item menu-item-idea">
          <a href="/categories/idea" rel="section">
            
            想法
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
            关于我
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/12/07/JavaScript之面向对象/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="彭丹丹">
      <meta itemprop="description" content>
      <meta itemprop="image" content="https://upload.jianshu.io/users/upload_avatars/830956/debe5156eed7?imageMogr2/auto-orient/strip|imageView2/1/w/240/h/240">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="春风自是人间客">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">JavaScript之面向对象</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-12-07T15:44:34+08:00">
                2020-12-07
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/program/" itemprop="url" rel="index">
                    <span itemprop="name">program</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1,884
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  9
                </span>
              
            </div>
          

          
          
        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="java对象"><a href="#java对象" class="headerlink" title="java对象"></a>java对象</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">// 创建类——“人”</span><br><span class="line">public class People&#123;</span><br><span class="line">    int age;</span><br><span class="line">&#125;</span><br><span class="line">// 创建类——“男人”</span><br><span class="line">public class MenPeople extends People &#123;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line">// 创建对象</span><br><span class="line">MenPeople xm = new MenPeople();</span><br><span class="line">xm.age = 15;</span><br></pre></td></tr></table></figure>
<hr>
<h1 id="js对象"><a href="#js对象" class="headerlink" title="js对象"></a>js对象</h1><p><code>var xm = {age:15}</code></p>
<p><a href="https://www.jianshu.com/p/edf4d665d0df" target="_blank" rel="noopener">https://www.jianshu.com/p/edf4d665d0df</a></p>
<p><a href="https://www.cnblogs.com/yanyunpiaomaio/p/11025444.html" target="_blank" rel="noopener">https://www.cnblogs.com/yanyunpiaomaio/p/11025444.html</a></p>
<h1 id="JavaScript面向对象"><a href="#JavaScript面向对象" class="headerlink" title="JavaScript面向对象"></a>JavaScript面向对象</h1><p><a href="http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_encapsulation.html" target="_blank" rel="noopener">参考</a></p>
<h2 id="封装"><a href="#封装" class="headerlink" title="封装"></a>封装</h2><h3 id="生成对象"><a href="#生成对象" class="headerlink" title="生成对象"></a>生成对象</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">function Cat(name,color)&#123;</span><br><span class="line">    this.name = name;</span><br><span class="line">    this.color = color;</span><br><span class="line">&#125;</span><br><span class="line">var cat1 = new Cat(&apos;大猫&apos;,&apos;黄色&apos;);</span><br><span class="line">var cat2 = new Cat(&apos;小猫&apos;,&apos;黑色&apos;);</span><br><span class="line"></span><br><span class="line">cat1; // Cat &#123;name: &quot;大猫&quot;, color: &quot;黄色&quot;&#125; 指向Cat对象</span><br><span class="line">cat2; // Cat &#123;name: &quot;小猫&quot;, color: &quot;黑色&quot;&#125; 指向Cat对象</span><br></pre></td></tr></table></figure>
<p><strong>相当于我们平时用的数组中的</strong>：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">var arr = new Array(3).fill(2);</span><br><span class="line">var brr = new Array(5).fill(8);</span><br><span class="line">arr; // (3) [2, 2, 2] 指向Array对象</span><br><span class="line">brr; // (5) [8, 8, 8, 8, 8] 指向Array对象</span><br></pre></td></tr></table></figure>
<p>只不过我们平时是直接用<code>var arr = [1,2]</code>的形式，和<code>new Array</code>是同一个意思。</p>
<h2 id="对象的构造函数"><a href="#对象的构造函数" class="headerlink" title="对象的构造函数"></a>对象的构造函数</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">function Cat(name,color)&#123;</span><br><span class="line">    this.name = name;</span><br><span class="line">    this.color = color;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这段代码里面的<code>this.name = name</code>就是构造函数，可以直接用es6语法糖的形式写：</p>
<h3 id="es6语法糖class"><a href="#es6语法糖class" class="headerlink" title="es6语法糖class"></a>es6语法糖class</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">class Cat&#123;</span><br><span class="line">    constructor(x,y)&#123;</span><br><span class="line">        this.x = x;</span><br><span class="line">        this.y = y;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">var cat1 = new Cat(&apos;大猫&apos;,&apos;黄色&apos;);</span><br><span class="line"></span><br><span class="line">cat1; // Cat &#123;name: &quot;大猫&quot;, color: &quot;黄色&quot;&#125; 指向Cat对象</span><br></pre></td></tr></table></figure>
<h3 id="constructor-NaN"><a href="#constructor-NaN" class="headerlink" title="constructor"></a>constructor</h3><p>所以，<code>cat1</code>实例含有<code>constructor</code>属性指向它(Cat)的<code>构造函数</code>。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cat1.constructor === Cat; // true</span><br></pre></td></tr></table></figure>
<p><strong>相当于我们平时用的数组中的</strong>：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[1,2].constructor === Array; // true</span><br></pre></td></tr></table></figure>
<p><strong>其它</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[2].constructor(); // []</span><br><span class="line">[2].constructor() === Array.prototype.constructor();</span><br></pre></td></tr></table></figure>
<h3 id="instanceof"><a href="#instanceof" class="headerlink" title="instanceof"></a>instanceof</h3><p><code>JavaScript</code>还提供了<code>instanceof</code>运算符，验证<code>原型对象(Cat)</code>与<code>实例对象(cat1)</code>之间的关系。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cat1 instanceof Cat; // true</span><br></pre></td></tr></table></figure>
<p><strong>相当于我们平时用的数组中的</strong>：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[1,2] instanceof Array; // true</span><br></pre></td></tr></table></figure>
<h2 id="原型对象添加方法"><a href="#原型对象添加方法" class="headerlink" title="原型对象添加方法"></a>原型对象添加方法</h2><p>并不能说对象有原型，只能说对象的构造器有原型。</p>
<h3 id="直接添加造成的问题"><a href="#直接添加造成的问题" class="headerlink" title="直接添加造成的问题"></a>直接添加造成的问题</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">function Cat(name,color)&#123;</span><br><span class="line">    this.name = name;</span><br><span class="line">    this.color = color;</span><br><span class="line">    this.type = &apos;猫科动物&apos;;</span><br><span class="line">    this.eat = function()&#123;</span><br><span class="line">        console.log(&apos;吃鱼&apos;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">var cat1 = new Cat(&apos;大猫&apos;,&apos;黄色&apos;);</span><br><span class="line">var cat2 = new Cat(&apos;小猫&apos;,&apos;黑色&apos;);</span><br><span class="line"></span><br><span class="line">cat1.eat == cat2.eat; // false</span><br></pre></td></tr></table></figure>
<p>此时eat方法占用了太多内存，并且它们没有指向同一个引用地址，永远不会相等。参考数组的其实是相等的。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[1].push == [2].push; // true</span><br></pre></td></tr></table></figure>
<h3 id="用prototype添加方法"><a href="#用prototype添加方法" class="headerlink" title="用prototype添加方法"></a>用prototype添加方法</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">function Cat(name,color)&#123;</span><br><span class="line">    this.name = name;</span><br><span class="line">    this.color = color;</span><br><span class="line">&#125;</span><br><span class="line">Cat.prototype.type = &apos;猫科动物&apos;;</span><br><span class="line">Cat.prototype.eat = function()&#123;</span><br><span class="line">    console.log(&apos;吃鱼&apos;)</span><br><span class="line">&#125;</span><br><span class="line">var cat1 = new Cat(&apos;大猫&apos;,&apos;黄色&apos;);</span><br><span class="line">var cat2 = new Cat(&apos;小猫&apos;,&apos;黑色&apos;);</span><br><span class="line"></span><br><span class="line">cat1.eat == cat2.eat; // true，它们是指向同一个内存地址下的方法</span><br></pre></td></tr></table></figure>
<p>(就算不定义Cat的prototype，Cat也自带有prototype属性)</p>
<h2 id="prototype模式的验证方法"><a href="#prototype模式的验证方法" class="headerlink" title="prototype模式的验证方法"></a>prototype模式的验证方法</h2><h3 id="判断对象和实例的关系isPrototypeOf"><a href="#判断对象和实例的关系isPrototypeOf" class="headerlink" title="判断对象和实例的关系isPrototypeOf"></a>判断对象和实例的关系<code>isPrototypeOf</code></h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Cat.prototype.isPrototypeOf(cat1); // true</span><br></pre></td></tr></table></figure>
<p><strong>相当于我们平时用的数组中的</strong>：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Array.prototype.isPrototypeOf([]); // true</span><br></pre></td></tr></table></figure>
<h3 id="判断是本地属性还是prototype属性"><a href="#判断是本地属性还是prototype属性" class="headerlink" title="判断是本地属性还是prototype属性"></a>判断是本地属性还是prototype属性</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cat1.hasOwnProperty(&apos;name&apos;); // true</span><br><span class="line">cat1.hasOwnProperty(&apos;type&apos;); // false</span><br></pre></td></tr></table></figure>
<h3 id="in"><a href="#in" class="headerlink" title="in"></a>in</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&apos;name&apos; in cat1; // true</span><br></pre></td></tr></table></figure>
<p><strong>相当于我们平时用的数组中的</strong>：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&apos;push&apos; in []; // true</span><br></pre></td></tr></table></figure>
<h3 id="proto"><a href="#proto" class="headerlink" title="proto"></a><strong>proto</strong></h3><p>一般情况下，实例对象的<code>__proto__</code>指向原型对象的<code>prototype</code>。<br><code>prototype</code>被实例的<code>__proto__</code>指向<br><code>__proto__</code>指向构造函数的<code>prototype</code><br><code>__proto__</code>存在于实例和构造函数的原型对象，而不是实例与构造函数。<br>如：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cat1.__proto__ === Cat.prototype; // true</span><br></pre></td></tr></table></figure>
<p><strong>相当于我们平时用的数组中的</strong>：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[].__proto__ === Array.prototype; // true</span><br></pre></td></tr></table></figure>
<p><strong>其它情况</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">function fn()&#123;&#125;;</span><br><span class="line">fn.__proto__ === Function.prototype; // true</span><br></pre></td></tr></table></figure>
<p>把函数当作对象时，生成它的函数就是<code>Function</code>原型对象。</p>
<ol>
<li><code>Function</code>原型对象也同样适用此规则：</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Function.__proto__ === Function.prototype; // true</span><br><span class="line">Function.prototype.__proto__ == Object.prototype; // true 为了不指向自身的Function.prototype造成循环引用</span><br></pre></td></tr></table></figure>
<ol>
<li><code>Object</code>函数也是一个<code>Function</code>函数：</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Object.__proto__ === Function.prototype; // true</span><br><span class="line">Object.prototype.__proto__ === null ; // true 为了不指向自身的Object.prototype造成循环引用</span><br></pre></td></tr></table></figure>
<p><code>Object.prototype.__proto__==null</code>是所有函数的终点</p>
<hr>
<h1 id="DOM也有原型链"><a href="#DOM也有原型链" class="headerlink" title="DOM也有原型链"></a>DOM也有原型链</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">&lt;html&gt;</span><br><span class="line">  &lt;head&gt;</span><br><span class="line">    &lt;title&gt;dom原型测试&lt;/title&gt;</span><br><span class="line">  &lt;/head&gt;</span><br><span class="line">  &lt;body&gt;</span><br><span class="line">    &lt;div id=&quot;test&quot;&gt;test dom&lt;/div&gt;</span><br><span class="line">    &lt;script type=&quot;text/javascript&quot;&gt;</span><br><span class="line">      HTMLElement.prototype.hello = function()&#123;</span><br><span class="line">        console.log(this);</span><br><span class="line">      &#125;</span><br><span class="line">      var div = document.getElementById(&apos;test&apos;);</span><br><span class="line">      div.hello();</span><br><span class="line">    &lt;/script&gt;</span><br><span class="line">  &lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>
<hr>
<h1 id="Object-create实现类继承和克隆对象"><a href="#Object-create实现类继承和克隆对象" class="headerlink" title="Object.create实现类继承和克隆对象"></a>Object.create实现类继承和克隆对象</h1><h2 id="Object-create实现类继承"><a href="#Object-create实现类继承" class="headerlink" title="Object.create实现类继承"></a>Object.create实现类继承</h2><h3 id="先看不用Object-create来实现继承"><a href="#先看不用Object-create来实现继承" class="headerlink" title="先看不用Object.create来实现继承"></a>先看不用Object.create来实现继承</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">function Pd()&#123;</span><br><span class="line">&#125;</span><br><span class="line">Pd.prototype = Array.prototype;</span><br><span class="line">Pd.prototype.constructor = Pd;</span><br><span class="line">var pdd = new Pd();</span><br><span class="line">pdd.push(3);</span><br><span class="line">console.log(pdd); // Pd [3] __proto__:Array(0)直接就是真正的数组的__proto__</span><br></pre></td></tr></table></figure>
<p>效果：</p>
<p><img src="https://wx2.sinaimg.cn/mw690/0069qZtTgy1gho48t4y6vj30970i275q.jpg" alt="image.png"></p>
<p>此时打印<code>Array.prototype.constructor</code>会发现变成了<code>undefined</code>，已经改动了原生的<code>Array</code>.</p>
<h3 id="用Object-create实现继承"><a href="#用Object-create实现继承" class="headerlink" title="用Object.create实现继承"></a>用Object.create实现继承</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">function Pd()&#123;</span><br><span class="line">&#125;</span><br><span class="line">Pd.prototype = Object.create(Array.prototype);</span><br><span class="line">Pd.prototype.constructor = Pd;</span><br><span class="line">var pdd = new Pd();</span><br><span class="line">pdd.push(3);</span><br><span class="line">console.log(pdd); // Pd [3] __proto__:Array[__proto__:Array(0)]就是__proto__里面包含真正的数组的__proto__</span><br></pre></td></tr></table></figure>
<p>效果：</p>
<p><img src="https://wx1.sinaimg.cn/mw690/0069qZtTgy1gho49dgk5zj30d30irjt4.jpg" alt="image.png"></p>
<h3 id="区别"><a href="#区别" class="headerlink" title="区别"></a>区别</h3><p><strong>写法</strong>：</p>
<p><code>Pd.prototype = Array.prototype;</code>和<code>Pd.prototype = Object.create(Array.prototype);</code></p>
<p><strong>返回值</strong>：</p>
<ul>
<li><code>Pd [3] __proto__:Array(0)</code>直接就是真正的数组的<code>__proto__</code>;</li>
<li><code>Pd [3] __proto__:Array[__proto__:Array(0)]</code>就是<code>__proto__</code>里面包含真正的数组的<code>__proto__</code>。</li>
</ul>
<h3 id="用Object-create实现继承自己的类并带参数"><a href="#用Object-create实现继承自己的类并带参数" class="headerlink" title="用Object.create实现继承自己的类并带参数"></a>用Object.create实现继承自己的类并带参数</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">function Cat(name,color)&#123;</span><br><span class="line">    this.name = name;</span><br><span class="line">    this.color = color;</span><br><span class="line">&#125;</span><br><span class="line">var cat1 = new Cat(&apos;大猫&apos;,&apos;黄色&apos;);</span><br><span class="line"></span><br><span class="line">function Pd(name,color)&#123;</span><br><span class="line">    Cat.call(this,name,color);</span><br><span class="line">&#125;</span><br><span class="line">Pd.prototype = Object.create(Cat.prototype);</span><br><span class="line">Pd.prototype.constructor = Pd;</span><br><span class="line">var pdd = new Pd(&apos;小猫&apos;,&apos;白色&apos;);</span><br><span class="line"></span><br><span class="line">console.log(cat1,pdd); // Cat &#123;name: &quot;大猫&quot;, color: &quot;黄色&quot;&#125; Pd &#123;name: &quot;小猫&quot;, color: &quot;白色&quot;&#125;</span><br></pre></td></tr></table></figure>
<h3 id="用原生写法实现继承自己的类并带参数"><a href="#用原生写法实现继承自己的类并带参数" class="headerlink" title="用原生写法实现继承自己的类并带参数"></a>用原生写法实现继承自己的类并带参数</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">function Cat(name,color)&#123;</span><br><span class="line">    this.name = name;</span><br><span class="line">    this.color = color;</span><br><span class="line">&#125;</span><br><span class="line">var cat1 = new Cat(&apos;大猫&apos;,&apos;黄色&apos;);</span><br><span class="line"></span><br><span class="line">function Pd(name,color)&#123;</span><br><span class="line">  Cat.call(this,name,color);</span><br><span class="line">&#125;</span><br><span class="line">Pd.prototype = Cat.prototype;</span><br><span class="line">Pd.prototype.constructor = Pd;</span><br><span class="line">var pdd = new Pd(&apos;小猫&apos;,&apos;白色&apos;);</span><br><span class="line">console.log(cat1,pdd); // Cat &#123;name: &quot;大猫&quot;, color: &quot;黄色&quot;&#125; Pd &#123;name: &quot;小猫&quot;, color: &quot;白色&quot;&#125;</span><br></pre></td></tr></table></figure>
<h2 id="用Object-create克隆对象"><a href="#用Object-create克隆对象" class="headerlink" title="用Object.create克隆对象"></a>用Object.create克隆对象</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">var obj1 = &#123;a:2,b:&#123;name:&apos;小明&apos;&#125;&#125;;</span><br><span class="line">var obj2 = Object.create(obj1);</span><br><span class="line">console.log(obj2); // &#123;&#125;</span><br><span class="line">obj2.a = 3;</span><br><span class="line">obj2.b.name = &apos;小红&apos;;</span><br><span class="line">console.log(obj1); // &#123;a:2,b:&#123;name:&apos;小红&apos;&#125;&#125;;</span><br></pre></td></tr></table></figure>
<p>结论：obj1对象中的一级对象a:2并没有受影响，但二级对象b已经受影响。所以<strong>Object.create克隆的对象也只能实现一级对象的深拷贝</strong>。</p>
<p>obj2的具体值：</p>
<p><img src="https://wx1.sinaimg.cn/mw690/0069qZtTgy1gho49w7kobj306102wjra.jpg" alt="image.png"></p>
<hr>
<h1 id="extends继承"><a href="#extends继承" class="headerlink" title="extends继承"></a>extends继承</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">class Cat&#123;</span><br><span class="line">    constructor()&#123;</span><br><span class="line">        console.log(&apos;cat&apos;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">class Child extends Cat&#123;</span><br><span class="line">&#125;;</span><br><span class="line">var cat = new Cat();</span><br><span class="line">var child = new Child();</span><br></pre></td></tr></table></figure>
<p>继承所有参数：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">class Cat&#123;</span><br><span class="line">    constructor(name)&#123;</span><br><span class="line">        this.name = name;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">class Child extends Cat&#123;</span><br><span class="line">    constructor(name)&#123;</span><br><span class="line">        super(name);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line">var cat = new Cat(&apos;1&apos;);</span><br><span class="line">var child = new Child(&apos;2&apos;);</span><br><span class="line">console.log(cat,child); // Cat &#123;name: &quot;1&quot;&#125; Child &#123;name: &quot;2&quot;&#125;</span><br></pre></td></tr></table></figure>
<hr>
<h1 id="new-target方法判断是否父类"><a href="#new-target方法判断是否父类" class="headerlink" title="new.target方法判断是否父类"></a><code>new.target</code>方法判断是否父类</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">class Cat&#123;</span><br><span class="line">    constructor()&#123;</span><br><span class="line">        console.log(new.target);</span><br><span class="line">        if (new.target === Cat) &#123;</span><br><span class="line">            console.log(&apos;父类&apos;);</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            console.log(&apos;子类&apos;);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">class Child extends Cat&#123;</span><br><span class="line">    constructor()&#123;</span><br><span class="line">        super();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line">var cat = new Cat();</span><br><span class="line">var child = new Child();</span><br></pre></td></tr></table></figure>
<hr>
<h1 id="new-Array-和-比较"><a href="#new-Array-和-比较" class="headerlink" title="new Array()和[]比较"></a>new Array()和[]比较</h1><h2 id="性能"><a href="#性能" class="headerlink" title="性能"></a>性能</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">var startTime=new Date().getTime();</span><br><span class="line">var a2 = new Object();</span><br><span class="line">for(var i = 0;i&lt;10000000;i++)&#123;</span><br><span class="line">    a2[i] = [];</span><br><span class="line">&#125;</span><br><span class="line">var endTime=new Date().getTime();</span><br><span class="line">console.log(&apos;[]输出耗时:&apos;,endTime-startTime);</span><br><span class="line"></span><br><span class="line">var startTime2=new Date().getTime();</span><br><span class="line">var a = new Object();</span><br><span class="line">for(var i = 0;i&lt;10000000;i++)&#123;</span><br><span class="line">    a[i] = new Array();</span><br><span class="line">&#125;</span><br><span class="line">var endTime2=new Date().getTime();</span><br><span class="line">console.log(&apos;new Array()输出耗时:&apos;,endTime2-startTime2);</span><br></pre></td></tr></table></figure>
<p>结果：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[]输出耗时: 304</span><br><span class="line">new Array()输出耗时: 600</span><br></pre></td></tr></table></figure>
<p>每次结果不同，但大约都是<code>new Array()</code>是<code>[]</code>的两倍，时间越大，差距越大。</p>
<p>(最好用时间差相减来计算时间，用<code>console.time</code>可能会有先后的问题导致不准确。)</p>
<h2 id="写法"><a href="#写法" class="headerlink" title="写法"></a>写法</h2><p><code>[]</code>是字面量，JSON格式的语法是引擎直接解释的；<br><code>new Array()</code>需要调用<code>Array</code>的构造器。</p>
<hr>
<h1 id="prototype-和-hasOwnProperty"><a href="#prototype-和-hasOwnProperty" class="headerlink" title="prototype 和 hasOwnProperty"></a>prototype 和 hasOwnProperty</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Array.prototype.arr = function()&#123;console.log(&apos;print arr&apos;)&#125;;</span><br><span class="line">var a = [1,2,3];</span><br><span class="line">a.arr(); // &apos;print arr&apos;</span><br><span class="line">Array.prototype.hasOwnProperty(&apos;arr&apos;); // true</span><br><span class="line">a.hasOwnProperty(&apos;arr&apos;); // false</span><br><span class="line">Array.hasOwnProperty(&apos;arr&apos;); // false</span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/12/07/CSS之常用方法总结/" rel="next" title="CSS之常用方法总结">
                <i class="fa fa-chevron-left"></i> CSS之常用方法总结
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2020/12/08/JavaScript之观察者模式(Obverse)/" rel="prev" title="JavaScript之观察者模式(Obverse)">
                JavaScript之观察者模式(Obverse) <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zODk2OC8xNTQ5Ng=="></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="https://upload.jianshu.io/users/upload_avatars/830956/debe5156eed7?imageMogr2/auto-orient/strip|imageView2/1/w/240/h/240" alt="彭丹丹">
            
              <p class="site-author-name" itemprop="name">彭丹丹</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">183</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#java对象"><span class="nav-number">1.</span> <span class="nav-text">java对象</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#js对象"><span class="nav-number">2.</span> <span class="nav-text">js对象</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#JavaScript面向对象"><span class="nav-number">3.</span> <span class="nav-text">JavaScript面向对象</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#封装"><span class="nav-number">3.1.</span> <span class="nav-text">封装</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#生成对象"><span class="nav-number">3.1.1.</span> <span class="nav-text">生成对象</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#对象的构造函数"><span class="nav-number">3.2.</span> <span class="nav-text">对象的构造函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#es6语法糖class"><span class="nav-number">3.2.1.</span> <span class="nav-text">es6语法糖class</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#constructor-NaN"><span class="nav-number">3.2.2.</span> <span class="nav-text">constructor</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#instanceof"><span class="nav-number">3.2.3.</span> <span class="nav-text">instanceof</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#原型对象添加方法"><span class="nav-number">3.3.</span> <span class="nav-text">原型对象添加方法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#直接添加造成的问题"><span class="nav-number">3.3.1.</span> <span class="nav-text">直接添加造成的问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#用prototype添加方法"><span class="nav-number">3.3.2.</span> <span class="nav-text">用prototype添加方法</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#prototype模式的验证方法"><span class="nav-number">3.4.</span> <span class="nav-text">prototype模式的验证方法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#判断对象和实例的关系isPrototypeOf"><span class="nav-number">3.4.1.</span> <span class="nav-text">判断对象和实例的关系isPrototypeOf</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#判断是本地属性还是prototype属性"><span class="nav-number">3.4.2.</span> <span class="nav-text">判断是本地属性还是prototype属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#in"><span class="nav-number">3.4.3.</span> <span class="nav-text">in</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#proto"><span class="nav-number">3.4.4.</span> <span class="nav-text">proto</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#DOM也有原型链"><span class="nav-number">4.</span> <span class="nav-text">DOM也有原型链</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Object-create实现类继承和克隆对象"><span class="nav-number">5.</span> <span class="nav-text">Object.create实现类继承和克隆对象</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Object-create实现类继承"><span class="nav-number">5.1.</span> <span class="nav-text">Object.create实现类继承</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#先看不用Object-create来实现继承"><span class="nav-number">5.1.1.</span> <span class="nav-text">先看不用Object.create来实现继承</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#用Object-create实现继承"><span class="nav-number">5.1.2.</span> <span class="nav-text">用Object.create实现继承</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#区别"><span class="nav-number">5.1.3.</span> <span class="nav-text">区别</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#用Object-create实现继承自己的类并带参数"><span class="nav-number">5.1.4.</span> <span class="nav-text">用Object.create实现继承自己的类并带参数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#用原生写法实现继承自己的类并带参数"><span class="nav-number">5.1.5.</span> <span class="nav-text">用原生写法实现继承自己的类并带参数</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#用Object-create克隆对象"><span class="nav-number">5.2.</span> <span class="nav-text">用Object.create克隆对象</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#extends继承"><span class="nav-number">6.</span> <span class="nav-text">extends继承</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#new-target方法判断是否父类"><span class="nav-number">7.</span> <span class="nav-text">new.target方法判断是否父类</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#new-Array-和-比较"><span class="nav-number">8.</span> <span class="nav-text">new Array()和[]比较</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#性能"><span class="nav-number">8.1.</span> <span class="nav-text">性能</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#写法"><span class="nav-number">8.2.</span> <span class="nav-text">写法</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#prototype-和-hasOwnProperty"><span class="nav-number">9.</span> <span class="nav-text">prototype 和 hasOwnProperty</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">彭丹丹</span>

  
</div>


	<div class="powered-by">
	<i class="fa fa-user-md"></i><span id="busuanzi_container_site_uv">
	  本站访客数:<span id="busuanzi_value_site_uv"></span>
	</span>
	</div>
  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  












  





  

  

  

  
  

  

  

  

  undefined
  
  <!-- 页面爆炸效果 -->
  
<script src="/live2dw/lib/L2Dwidget.min.js?0c58a1486de42ac6cc1c59c7d98ae887"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/tororo.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true}});</script></body>
</html>
